package list

import (
	"errors"
)

type Node struct {
	Data int
	Next *Node
}

type LinkedList struct {
	Len  int
	Head *Node
}

func (i *LinkedList) Pop() *Node {
	if i.Head == nil {
		return nil
	}

	next := i.Head.Next

	for next.Next.Next != nil {
		next = next.Next
	}

	var tmp *Node
	tmp = next.Next
	next.Next = nil
	i.Len--
	return tmp

}
func (p *LinkedList) Append(node *Node) error {
	if node == nil {
		return errors.New("node is nil")
	}

	if p.Head == nil {
		p.Head = node
	} else {
		currNode := p.Head
		for currNode.Next != nil {
			currNode = currNode.Next
		}
		currNode.Next = node
	}
	p.Len += 1
	return nil
}

func (p *LinkedList) Show() {
	var n = p.Head
	for n != nil {
		println("node data : ", n.Data)
		n = n.Next
	}

}

func (p *LinkedList) Reverse() {
	var prev, next *Node
	cur := p.Head

	for cur != nil {
		next = cur.Next
		cur.Next = prev
		prev = cur
		cur = next
	}

	p.Head = prev
}
